﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ParserColumnasDescripcionMarca
{
    public partial class Parser : Form
    {
        ExcelApp MyExcelApp = null;
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        Thread oThread = new Thread(new ThreadStart(ShowProgress));

        public Parser()
        {
            InitializeComponent();
        }

        public static void ShowProgress()
        {
            Process MyForm = new Process();
            MyForm.StartPosition = FormStartPosition.CenterScreen;
            MyForm.ShowDialog();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ExcelDialog = new OpenFileDialog();

            ExcelDialog.Filter = "Excel Files (*.xlsx) | *.xlsx";
            ExcelDialog.InitialDirectory = @"C:\";
            ExcelDialog.Title = "Select your excel";
            if (ExcelDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(ExcelDialog.FileName))
            {
                txtFileName.Text = ExcelDialog.FileName;
                MyExcelApp = new ExcelApp(txtFileName.Text);
                cboAvailableSheets.DataSource = MyExcelApp.ExcelSheets;
                groupBox1.Enabled = false;
                groupBox2.Enabled = true;
            }
        }

        private void btnStepIIProcess_Click(object sender, EventArgs e)
        {
            cboColumnA.DataSource = MyExcelApp.GetColumns(cboAvailableSheets.Text);
            cboColumnB.DataSource = MyExcelApp.GetColumns(cboAvailableSheets.Text);

            saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Export Excel File To";

            groupBox2.Enabled = false;
            groupBox3.Enabled = true;
        }

        private void btnStepIIIProcess_Click(object sender, EventArgs e)
        {
            cboAvailableColumns.DataSource = MyExcelApp.GetColumns(cboAvailableSheets.Text);

            groupBox3.Enabled = false;
            groupBox4.Enabled = true;
        }

        private void btnStepIVProcess_Click(object sender, EventArgs e)
        {
            MyExcelApp.Process(cboColumnA.Text, cboColumnB.Text);
            var _result = MyExcelApp.GetExcelData(cboAvailableColumns.Text, cboAvailableValuesDistinct.Text, false);
            var _resultWithOut = MyExcelApp.GetExcelData(cboAvailableColumns.Text, cboAvailableValuesDistinct.Text, true);
            dataGridView1.DataSource = _result;
            dataGridView2.DataSource = _resultWithOut;
            groupBox4.Enabled = false;
            groupBox5.Enabled = true;
        }
        
        private void btnProcessAndSave_Click(object sender, EventArgs e)
        {
            saveFileDialog.Title = string.Format("Nombre Excel sin {0}", cboAvailableValuesDistinct.Text);
            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Visible = false;
                oThread = new Thread(new ThreadStart(ShowProgress));
                oThread.Start();

                MyExcelApp.OnProcess(dataGridView1, saveFileDialog.FileName,chkGeneratePT.Checked);

                if (oThread != null && oThread.IsAlive)
                {
                    oThread.Abort();
                    oThread = null;
                }
                this.Visible = true;
                MessageBox.Show("Excel file I created");
            }

            saveFileDialog.Title = string.Format("Nombre Excel con unicamente {0}", cboAvailableValuesDistinct.Text);
            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Visible = false;
                oThread = new Thread(new ThreadStart(ShowProgress));
                oThread.Start();

                MyExcelApp.OnProcess(dataGridView2, saveFileDialog.FileName, chkGeneratePT.Checked);

                if (oThread != null && oThread.IsAlive)
                {
                    oThread.Abort();
                    oThread = null;
                }
                this.Visible = true;
                MessageBox.Show("Excel file II created");
            }

            MyExcelApp.KillProcess();
        }



        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void cboAvailableColumns_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboAvailableValuesDistinct.DataSource = MyExcelApp.GetExcelDataByColumn(cboAvailableColumns.Text);
        }

        private void btnGenerateReport_Click(object sender, EventArgs e)
        {
            ReportShower rs = new ReportShower(GetDS(MyExcelApp.ExcelData));
            rs.ShowDialog();
        }

        private string GetDS(List<CustomExcelData> Productos)
        {
            string final = string.Empty;
            string _Cat = "<h3>{0}</h3> <br/>";
            string _SubCat = "<h4>{0}</h4> <br/>";
            string _Det = "{0} {1} {2} {3} <br/>";

            for (int i = 0; i < Productos.Count; i++)
            {
                if (i == 0)
                    final = InicialWhen0(Productos, final, _Cat, _SubCat, _Det, i);
                else
                {
                    if (Productos[i].FLIA_DESC != Productos[i - 1].FLIA_DESC) //Cambio la Categoria                    
                        final += string.Format(_Cat, Productos[i].FLIA_DESC);
                    if (Productos[i].SUB_FLIA_DESC != Productos[i - 1].SUB_FLIA_DESC) // Cambio la subcategoria                    
                        final += string.Format(_SubCat, Productos[i].SUB_FLIA_DESC);

                    final += string.Format(_Det, Productos[i].COMBINADA, Productos[i].PRES, Productos[i].CLAS_LTA, Productos[i].PRECIO);
                }
            }
            return final;
        }

        private static string InicialWhen0(List<CustomExcelData> Productos, string final, string _Cat, string _SubCat, string _Det, int i)
        {
            final += string.Format(_Cat, Productos[i].FLIA_DESC);
            final += string.Format(_SubCat, Productos[i].SUB_FLIA_DESC);
            foreach (var item in Productos.Where(x => x.FLIA_DESC == Productos[i].FLIA_DESC && x.SUB_FLIA_DESC == Productos[i].SUB_FLIA_DESC))
                final += string.Format(_Det, item.COMBINADA, item.PRES, item.CLAS_LTA, item.PRES);
            return final;
        }


    }
}
